var EVENT_REGISTER=[];

function RegisterEventHandler(name,handler){
	var found=false;
	$.each(EVENT_REGISTER,function(a,b){
		if(b.name===name){
			found=true;
			return;
		}
	});
	if(!found)
	EVENT_REGISTER.push({"handler":handler,"name":name});
}

function FIRE_EVENT(data){
	if(EVENT_REGISTER.length>0){
		$.each(EVENT_REGISTER,function(a,b){
			//fire only if b is type of function
			if('function' === (typeof b.handler)){
				b.handler(data,b.name);
			}
		});
	}
}

//Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});

google.setOnLoadCallback(function (){
	//angular.bootstrap(document.body, ['MicroCampusApplication']);
});
//navigate through tabs on user dashboard data page.
adminPanel.controller("UserDashBoardTabsController",['cacheFactory','$scope','$rootScope',function($cacheFactory,$scope,$rootScope){
	this.selectedTab=0;
	this.contentUrl="";
	$rootScope.contentUrl=""
	this.setTabAction=function(tab,url){
		this.selectedTab=tab;
		this.contentUrl=url;//the the data below tabbed pane
		$rootScope.contentUrl=url;
	};

	this.isTabSelected=function(tab){
		return this.selectedTab===tab;
	}

}]);

adminPanel.controller("UserDashBoardController",['cacheFactory','$http','$scope','$rootScope','$q','$filter',function(cacheFactory,$http,$scope,$rootScope,$q,$filter){
	$scope.isAcademicRendering=false;
	$scope.noGradeFound=true;
	this.msg="ok";
	this.userId=0;
	$scope.selectedStudent="";
	var  thisObject=this;
	//use to send data to server
	$rootScope.searchCriteria={studentEntityId:0,pageNumber:1,limit:3};
	$scope.studentProfiles=null;
	$scope.selectedStudentProfile=null;
	
	this.mainContentUrl="";
	this.setPage=function(url){
		this.mainContentUrl=url;
	};	
	this.initUserDashboard=function(userId){
		thisObject.userId=userId;
		thisObject.getStudentInformation(userId);
	};
	
	//get information about child linked with parent user id
	this.getStudentInformation=function(userId){
		var defer = $q.defer();
		defer.promise.then(function () {
			FIRE_EVENT({"e":'STUDENT_PROFILE_LOADED',"d":$scope.studentProfiles})	
		});
		$rootScope.searchCriteria.userId=userId;
		$http.post(BASE_URL+"user/handleRequest/USER_DASHBOARD_GET_STUDENT_DATA",$rootScope.searchCriteria).success(function(data){
			if(data.status=='SUCCESS'){
				$scope.studentProfiles=data.result;
				$rootScope.searchCriteria.studentEntityId=$scope.studentProfiles[0].entityId;
				$scope.selectedStudent=$scope.studentProfiles[0].firstName +" " + $scope.studentProfiles[0].lastName;
				$scope.selectedStudentProfile=$scope.studentProfiles[0];
				thisObject.checkGradeMapping();
				defer.resolve();
			}else{
				alert(data.status +":"+data.msg);
			}
		}).error(function(){
			ERROR_LOGGER("Server Error");
		});
	};
	
	this.checkGradeMapping=function(){
		$scope.noGradeFound=(!$scope.selectedStudentProfile.totalGradeInfo || $scope.selectedStudentProfile.totalGradeInfo.length<=0)?true:false
	};
	
	this.selectStudent=function(id){
		var  selected = $filter('filter')($scope.studentProfiles, {entityId: id});
		$rootScope.searchCriteria.studentEntityId=selected[0].entityId;
		$scope.selectedStudent=selected[0].firstName +" "+ selected[0].lastName;
		$scope.studentProfile=selected[0];
		$scope.selectedStudentProfile=$scope.studentProfile;
		//this is important
		thisObject.checkGradeMapping();
		
		FIRE_EVENT({"e":'STUDENT_PROFILE_CHANGED',"d":$scope.selectedStudentProfile,"url":thisObject.mainContentUrl})
	};
	
	this.getSearchCriteria=function(){
		return $rootScope.searchCriteria;
	}
}]);
